package com.gzhryc.shared_device.oem.h5.mng.controllers.place;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.oem.code.place.dao.db.Clerk;
import com.gzhryc.shared_device.oem.code.place.dao.db.ClerkFuncModel;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceClerk;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.dao.models.ClerkFuncModelCount;
import com.gzhryc.shared_device.oem.code.place.dao.models.ClerkPlaceCount;
import com.gzhryc.shared_device.oem.code.place.services.ClerkFuncModelService;
import com.gzhryc.shared_device.oem.code.place.services.ClerkService;
import com.gzhryc.shared_device.oem.code.place.services.PlaceClerkService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.place.services.dto.ClerkSearch;
import com.gzhryc.shared_device.oem.code.place.services.dto.PlaceClerkSearch;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.code.user.services.UserService;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class ClerkController {
	
	@Route(value = {"/clerk/list.html"})
    public String list(HttpServletRequest request) {
        return "/clerk/list.html";
    }

    @Route(value = "/clerk/list.data")
    public ListResult<Clerk> listData(HttpServletRequest request) {
        ListResult<Clerk> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        ClerkSearch search = WebUtils.getParameter(request, ClerkSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(ClerkService.self().countJoinBySearch(search));
        List<Clerk> clerkList = ClerkService.self().findJoinBySearch(search, pageNo, pageLimit);
        
        if (clerkList != null && clerkList.size() > 0) {
        	List<Long> merchantUserIds = new ArrayList<>();
        	List<Long> userIds = new ArrayList<>();
        	for(Clerk clerk : clerkList) {
        		merchantUserIds.add(clerk.getMerchantUserId());
        		userIds.add(clerk.getUserId());
        	} 
        	
        	List<ClerkPlaceCount> clerkPlaceCountList = PlaceClerkService.self().findPlaceCountByUserKeys(merchantUserIds,userIds);
        	
        	List<ClerkFuncModelCount> clerkFuncModeCountList = ClerkFuncModelService.self().findFuncModelCountByUserKeys(merchantUserIds,userIds);
        	
        	List<User> merchantUserList = UserService.self().findByUserIds(merchantUserIds);
        	
        	for(Clerk clerk : clerkList) {
        		if(merchantUserList != null && merchantUserList.size() > 0) {
	        		for(User user : merchantUserList) {
	        			if(clerk.getMerchantUserId().equals(user.getId())) {
	        				clerk.setMerchant(user);
	        				break;
	        			}
	        		}
        		}
        		clerk.setPlaceCount(0L);
        		if(clerkPlaceCountList != null && clerkPlaceCountList.size() > 0) {
    	        	for(ClerkPlaceCount clerkPlaceCount : clerkPlaceCountList) {
    	        		if(clerkPlaceCount.getMerchantUserId().equals(clerk.getMerchantUserId())) {
    	        			if(clerkPlaceCount.getUserId().equals(clerk.getUserId())) {
    	        				clerk.setPlaceCount(clerkPlaceCount.getPlaceCount());
    	        				break;
    	        			}
    	        		}
    	        	}
            	}
        		
        		clerk.setFuncModelCount(0L);
        		if(clerkFuncModeCountList != null && clerkFuncModeCountList.size() > 0) {
    	        	for(ClerkFuncModelCount clerkFuncModelCount : clerkFuncModeCountList) {
    	        		if(clerkFuncModelCount.getMerchantUserId().equals(clerk.getMerchantUserId())) {
    	        			if(clerkFuncModelCount.getUserId().equals(clerk.getUserId())) {
    	        				clerk.setFuncModelCount(clerkFuncModelCount.getFuncModelCount());
    	        				break;
    	        			}
    	        		}
    	        	}
            	}
        	}
        		
            result.setCode(DtoConstants.SUCCESS, null);
        	result.getBody().setData(clerkList);
        }
        return result;
    }
    
    @Route(value = { "/clerk/info.html" })
    public String info(HttpServletRequest request) {
        Long userId = WebUtils.getLong(request,"userId");
        Long merchantUserId = WebUtils.getLong(request,"merchantUserId");
        Clerk clerk = ClerkService.self().getJoinByKeys(userId,merchantUserId);
        if(clerk != null) {
        	WebUtils.setAttribute(request,clerk);
        	User merchant = UserService.self().getById(clerk.getMerchantUserId());
        	request.setAttribute("merchant", merchant);
        }
        return "/clerk/info.html";
    }

    @Route(value = "/clerk/place/list.data")
    public ListResult<PlacePhysicalStore> placeListData(HttpServletRequest request){
        ListResult<PlacePhysicalStore> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long userId = WebUtils.getLong(request,"userId");
        Long merchantUserId = WebUtils.getLong(request,"merchantUserId");
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        if(userId != null && merchantUserId != null) {
        	PlaceClerkSearch search = new PlaceClerkSearch();
            search.setUserId(userId);
            search.setMerchantUserId(merchantUserId);
            
          	result.getBody().setTotal(PlaceClerkService.self().countBySearch(search));
            List<PlaceClerk> placeClerkList = PlaceClerkService.self().findBySearch(search, pageNo, pageLimit);
            if(placeClerkList != null && placeClerkList.size() > 0) {
            	List<Long> placeIds = new ArrayList<>();
            	for(PlaceClerk placeClerk : placeClerkList) {
            		placeIds.add(placeClerk.getPlaceId());
            	}
            	
            	List<PlacePhysicalStore> data = PlacePhysicalStoreService.self().findJoinByIds(placeIds);
            	if (data != null && data.size() > 0) {
                     result.setCode(DtoConstants.SUCCESS, null);
                     result.getBody().setData(data);
                }
            }
        }
        return result;
    }

    @Route(value = "/clerk/func_model/list.data")
    public ListResult<ClerkFuncModel> funcModelListData(HttpServletRequest request) {
        ListResult<ClerkFuncModel> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long userId = WebUtils.getLong(request,"userId");
        Long merchantUserId = WebUtils.getLong(request,"merchantUserId");
        if(userId != null && merchantUserId != null) {
	        result.getBody().setData(ClerkFuncModelService.self().findJoinByUserKeys(userId,merchantUserId));
	        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
	            String resDomain = PropertyTools.get().getString("resDomain");
	            for(ClerkFuncModel item : result.getBody().getData()){
	                if(StringTools.isNotBlank(item.getFuncModel().getIconUrl())){
	                    item.getFuncModel().setIconUrl(resDomain + item.getFuncModel().getIconUrl());
	                }
	            }
	            result.setCode(DtoConstants.SUCCESS, null);
	        }
        }
        return result;
    }
}
